var area;
layui.use(['element', 'jquery','drawer'], function() {
    var drawer = layui.drawer;
    var element = layui.element;
    var $ = layui.jquery;

    var email = localStorage.getItem('email');
    var nickname = localStorage.getItem('nickname');
    $("[name=email]").val(email);
    $("[name=nickname]").val(nickname);

    $(function () {
        $("#comment-container").load('../comment/' + blogId);
        if(location.href.startsWith('https')){
            const meta = document.createElement('meta');
            meta.content = 'upgrade-insecure-requests';
            meta.httpEquiv = "Content-Security-Policy";
            document.getElementsByTagName('head')[0].appendChild(meta);
        }

    });
    $('.search-icon').on('click', function () {
        window.open('/blog/blog/article?s='+$('#searchtxt').val());
    });
    $('#searchtxt').on('keyup',function(event){
        if(event.keyCode === 13){
            $('.search-icon').click();
        }
    });
    $('.menu.toggle').click(function () {
        $('.m-item').toggleClass('m-mobile-hide');
    });
    tocbot.init({
        // Where to render the table of contents.
        tocSelector: '.js-toc',
        // Where to grab the headings to build the table of contents.
        contentSelector: '.js-toc-content',
        // Which headings to grab inside of the contentSelector element.
        headingSelector: 'h1, h2, h3',
    });

    $('.buttons .button').click(function () {
        drawer.open({
            direction: "right",
            dom: ".toc-container",
            distance: "40%"
        });
    });

    $('#toTop-button').click(function () {
        $(window).scrollTo(0,500);
    });


    var waypoint = new Waypoint({
        element: document.getElementById('waypoint'),
        handler: function(direction) {
            if (direction == 'down') {
                $('#toolbar').show(100);
            } else {
                $('#toolbar').hide(500);
            }
            console.log('Scrolled to waypoint!  ' + direction);
        }
    })

    $('html,body').animate({

            scrollTop: ($(decodeURIComponent(window.location.hash)).offset().top - 60) + "px"

        },

        300);
    $('.toc-list-item a').click(function() {
        f(this);
    });
    $('.pl').click(function() {
        f(this);
    });
});

function f(that) {
    var target = document.getElementById(decodeURIComponent(that.hash).slice(1));

    if (!target) return;

    var targetOffset = $(target).offset().top - 60;

    $('html,body').animate({

            scrollTop: targetOffset

        },

        300);

    return false;
}
function a(el) {

    $("[name=replyEmail]").val(localStorage.getItem('email'));
    $("[name=ReplyNickname]").val(localStorage.getItem('nickname'));
    var targetId = $(el).data('targetid')
        , targetName = $(el).data('targetname')
        , $container = $('.comment-parent').siblings('.replycontainer');
    if ($(el).text() == '回复') {
        $container.find('textarea').attr('placeholder', '回复【' + targetName + '】');
        $container.removeClass('layui-hide');
        $container.find('input[name="parentCommentId"]').val(targetId);
        $(el).parents('.blog-comment li').find('.btn-reply').text('回复');
        $(el).text('收起');
    } else {
        $container.addClass('layui-hide');
        $container.find('input[name="targetUserId"]').val(0);
        $(el).text('回复');
    }
}

function tj () {
    let email = localStorage.getItem('email');
    let nickname = localStorage.getItem('nickname');
    if(email == null){
        localStorage.setItem("email", $("[name=email]").val())
    }
    if(nickname == null ){
        localStorage.setItem("nickname", $("[name=nickname]").val())
    }
    let content = $("[name='content']").val();
    if(content == null || content == ''){
        layer.msg("请输入内容");
        return ;
    }
    $("#comment-container").load('../comment/post', {
        "blogId" : $("[name='blogId']").val(),
        "nickname": nickname==null ? $("[name='nickname']").val() : nickname,
        "email"   : email==null ? $("[name='email']").val(): email,
        "content" : content
    }, function (responseTxt, statusTxt, xhr) {
        clearContent();
    });
}

function b () {
    let email = localStorage.getItem('email');
    let nickname = localStorage.getItem('nickname');
    let replyContent = $("[name='replyContent']").val();
    if(replyContent == null || replyContent == ''){
        layer.msg("请输入内容");
        return;
    }
    $("#comment-container").load('../comment/post', {
        "blogId" : $("[name='blogId']").val(),
        "parentCommentId" : $("[name='parentCommentId']").val(),
        "nickname": nickname== null ? $("[name='nickname']").val() : nickname,
        "email"   : email==null ? $("[name='email']").val() : email,
        "content" : replyContent
    }, function (responseTxt, statusTxt, xhr) {
        clearContent();
    });
}
function clearContent() {
    $("[name='replyContent']").val('');
    $("[name='content']").val('');
    $("[name='parentCommentId']").val(-1);
    $("[name='replyContent']").attr("placeholder", "请输入回复信息...");
    $("[name='content']").attr("placeholder", "请输入评论信息...");
}
